package com.huluobo.high3videoproject.utils;


import android.content.Context;

import com.huluobo.high3videoproject.bean.UserInfo;
import com.huluobo.high3videoproject.bean.UserMoney;
import com.huluobo.high3videoproject.db.DaoMaster;
import com.huluobo.high3videoproject.db.DaoSession;
import com.huluobo.high3videoproject.db.UserInfoDao;
import com.huluobo.high3videoproject.db.UserMoneyDao;
import com.umeng.commonsdk.debug.D;

import java.util.List;

/**
 * 用于数据库对象的增删改查
 * 1.GreenDao数据库
 * 2.Room数据库
 */
public class DBUtils {
    private static volatile DBUtils dbUtils;

    private DaoSession daoSession;

    private UserMoneyDao userMoneyDao;

    private UserInfoDao userInfoDao;

    private DBUtils() {

    }

    public static DBUtils getInstance() {
        if (dbUtils == null) {
            synchronized (DBUtils.class) {//添加同步锁,需要判断2次是否为空
                if (dbUtils == null) {
                    dbUtils = new DBUtils();
                }
            }
        }
        return dbUtils;
    }


    /**
     * 初始化数据库
     *
     * @param context 上下文
     */
    public void init(Context context) {
        //1.数据库操作实体
        daoSession = DaoMaster.newDevSession(context, "High3Demo.db");
        //2.那大表的实体
        userMoneyDao = daoSession.getUserMoneyDao();
        userInfoDao = daoSession.getUserInfoDao();
    }

    /**
     * 插入数据
     *
     * @param userMoney 用户对象
     */
    public void insertUser(UserMoney userMoney) {
        if (userMoney != null) {
            userMoneyDao.insert(userMoney);
        }
    }

    public void insertUserInfo(UserInfo userInfo) {
        if (userInfo != null) {
            userInfoDao.insert(userInfo);
        }
    }

    /**
     * 删除数据
     *
     * @param userMoney 用户对象
     */
    public void deleteUser(UserMoney userMoney) {
        if (userMoney != null) {
            userMoneyDao.delete(userMoney);
        }
    }

    /**
     * 修改数据
     *
     * @param userMoney 用户对象
     */
    public void updateUser(UserMoney userMoney) {
        if (userMoney != null) {
            userMoneyDao.update(userMoney);
        }
    }

    public void updateUserInfo(UserInfo userInfo) {
        if (userInfo != null) {
            userInfoDao.update(userInfo);
        }
    }

    /**
     * 查询用户信息
     *
     * @param name 用户名
     * @return 该id的用户所有信息
     */
    public UserMoney queryUser(String name) {
        UserMoney userMoney = null;
        if (name != null) {
            List<UserMoney> userMonies = userMoneyDao.loadAll();
            for (UserMoney user : userMonies) {
                if (user.getUserId().equals(name)) {
                    userMoney = user;
                    break;
                }
            }
        }
        return userMoney;
    }

    public UserInfo queryUserInfo(String name) {
        UserInfo userInfo = null;
        if (name != null) {
            List<UserInfo> userInfos = userInfoDao.loadAll();
            for (UserInfo userInfo1 : userInfos) {
                if (userInfo1.getUserName().equals(name)) {
                    userInfo = userInfo1;
                }
            }
        }
        return userInfo;
    }

}
